package com.kzd.gtgame.main.game


import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.createViewTextBottom
import com.kzd.gtgame.custom.createViewTextMove
import com.kzd.gtgame.custom.setTransitionStatusE
import com.kzd.gtgame.jetpack.vm.GameViewModel
import com.kzd.gtgame.main.game.adapter.GameDiscussGroupAdapter
import com.kzd.gtgame.main.toDiscussMoreActivity
import com.kzd.gtgame.main.toDiscussReplyActivity
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.include_app_recycler.*

class GameDiscussFragment : OwnerViewModeFragment<GameViewModel>() {

    companion object {
        fun newInstance(): GameDiscussFragment {
            return GameDiscussFragment()
        }
    }

    private val mDiscussGroupAdapter = GameDiscussGroupAdapter()

    override fun viewModel(): GameViewModel {
        return createViewModel(requireActivity())
    }

    override val layoutResId: Int
        get() = R.layout.include_app_recycler

    override fun initView() {
        rvAppRecyclerBody.apply {
            layoutManager = LinearLayoutManager(requireContext())
            itemAnimator = null
        }.adapter = mDiscussGroupAdapter

        mDiscussGroupAdapter.setOnItemClickListener { _, _, position ->
            val itemData = mDiscussGroupAdapter.getItem(position)
            val gameId = viewModel.gameDetailsLiveData.value
                ?.data?.data?.id ?: return@setOnItemClickListener
            val gameName = viewModel.gameDetailsLiveData.value
                ?.data?.data?.name ?: return@setOnItemClickListener
            toDiscussReplyActivity(gameName, gameId, itemData)
        }

        mDiscussGroupAdapter.setOnItemChildClickListener { _, view, position ->
            val itemData = mDiscussGroupAdapter.getItem(position)
            if (view.id == R.id.tvGameDiscussLike) {
                viewModel.gameDiscussGreat(itemData.id, itemData.is_zan, position)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.gameDiscuss(viewModel.mGameId)
    }

    override fun initViewMode() {
        viewModel.gameDiscussLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusE(response, mDiscussGroupAdapter)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mDiscussGroupAdapter.setNewInstance(data)
            mDiscussGroupAdapter.removeAllFooterView()
            if (data.size >= 10) {
                mDiscussGroupAdapter.addFooterView(mFooterViewMore)
            }
            mDiscussGroupAdapter.addFooterView(mFooterViewBottom)
        })

        viewModel.discussGreatLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data
                ?: return@observe
            val discuss = viewModel.gameDiscussLiveData.value
                ?.data?.data?.get(data.position)
                ?: return@observe
            discuss.apply {
                this.is_zan = data.isGreat
                this.zan = if (data.isGreat == 1)
                    (zan.toInt() + 1).toString()
                else (zan.toInt() - 1).toString()
            }
            mDiscussGroupAdapter.setData(data.position, discuss)
        })
    }

    private val mFooterViewMore: View by lazy {
        createViewTextMove(
            context = requireContext(),
            text = "点击查看更多评论>",
            textColor = R.color.colorMain
        ).apply {
            setOnClickListener {
                val gameId = viewModel.gameDetailsLiveData.value
                    ?.data?.data?.id ?: return@setOnClickListener
                val gameName = viewModel.gameDetailsLiveData.value
                    ?.data?.data?.name ?: return@setOnClickListener
                toDiscussMoreActivity(gameName, gameId)
            }
        }
    }

    private val mFooterViewBottom: View by lazy {
        createViewTextBottom(context = requireActivity())
    }
}